const {
  Base64,
  encode
} = require('./base64');
const CryptoJS = require('./hmac-sha1');

function formatTime(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds();


  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}

function getUptoken(filename) {
  const putPolicy = `{
  "scope": "appandroid:${filename}",
  "deadline":1697211672,
  "returnBody":
  "{
    \"name\":$(fname),
    \"size\":$(fsize),
    \"w\":$(imageInfo.width),
    \"h\":$(imageInfo.height),
    \"hash\":$(etag)
   }"
}`;

  const encodedPutPolicy = Base64.encode(putPolicy)
  const sign = CryptoJS.HmacSHA1(encodedPutPolicy, "vgqPPk4qqAeJoD6hbtt3a9knV_-xNoqXAt8Uqkim");
  console.log('sign', sign);

  const encodedSign = Base64.encode(sign);

  const uploadToken = 'KfINzZASI3aoGTYkr-sYzzzRcxhNMXV0dQJTtDrK' + ':' + encodedSign + ':' + encodedPutPolicy
  return uploadToken;
}
// rpx 转px
function rpx2px(rpx) {
  return rpx * wx.getSystemInfoSync().windowWidth/750;
}
 // px 转rpx
function px2rpx(px) {
  return 750/wx.getSystemInfoSync().windowWidth * px;
}

module.exports = {
  formatTime: formatTime,
  getUptoken,
  rpx2px,
  px2rpx
}